package com.briup.product_source.aop;

import com.briup.product_source.bean.BaseLog;
import com.briup.product_source.dao.BaseLogMapper;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Author lining
 * @Date 2023/6/18
 */
@Component
@Aspect
public class LogAspect {
    @Autowired
    private BaseLogMapper mapper;

    @Pointcut("execution(* com.briup.product_source.web..*.*(..)) && @annotation(Logging)")
    public void logPointCut(){
        //切入点

    }
    @Before("logPointCut()")
    public void beforeAdvice(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //请求方法 请求url ip地址 从请求对象中获取
        String method = request.getMethod();
        String uri = request.getRequestURI();

        //用户名 真实名字 从token获取
        String token = request.getHeader("token");
        //解析token获取用户名 真实姓名

        //日志时间： 创建new Date()表示
        Date date = new Date();
        //新建日志对象，保存数据库
        BaseLog baseLog = new BaseLog();
        baseLog.setLogRequestMethod(method);
        baseLog.setLogRequestUri(uri);
        baseLog.setLogTime(date);
        //补全代码....
        mapper.insert(baseLog);
    }

}
